package com.cn.yygh.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.yygh.common.result.R;
import com.cn.yygh.hosp.service.HospitalSetService;
import com.cn.yygh.model.hosp.HospitalSet;
import com.cn.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin//解决跨域
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(description = "医院设置接口")
@Slf4j //lombok
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;

    //    批量删除
    @DeleteMapping("/deleteByIds")
    @ApiOperation("批量删除")
    public R deleteIds(@ApiParam(name = "ids", value = "批量删除的医院的id") @RequestBody List<Long> ids) {
        boolean b = hospitalSetService.removeByIds(ids);

        return b ? R.ok().message("批量删除成功") : R.err().message("批量删除失败");
    }

    //查询所有的医院信息
    @ApiOperation("显示所有医院信息")
    @GetMapping("/findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        if (list.isEmpty()||list == null) {
            return R.err().message("没有要的数据");
        }
        return R.ok().data("list",list);
    }

    //分页查询
    @ApiOperation("分页查询")
    @PostMapping("/{pageNum}/{pageSize}")
    public R selectPage(@ApiParam(name = "pageNum", value = "第几页") @PathVariable Long pageNum,
                        @ApiParam(name = "pageSize", value = "每页显示多少条数据") @PathVariable Long pageSize,
                        @ApiParam(name = "hospitalSetQueryVo", value = "传入的请求体参数(医院名称--(模糊查询),医院编号--(唯一))") @RequestBody HospitalSetQueryVo hospitalSetQueryVo) {
        //1、创建page分页对象(如果传入的小于1的值，默认也是从1开始)
        Page<HospitalSet> hospitalSetPage = new Page<>(pageNum, pageSize);


        //封装查询条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

//        传入的医院code
        String hosCode = hospitalSetQueryVo.getHoscode();
//        传入医院的名字
        String hosName = hospitalSetQueryVo.getHosname();

//        做判断  作用类似于动态sql
        if (!StringUtils.isEmpty(hosCode)) {
            queryWrapper.eq("hoscode", hosCode); // columen 是表中对应的字段名
        }
        if (!StringUtils.isEmpty(hosName)) {
            queryWrapper.like("hosname", hosName);
        }

//        调用page
        Page<HospitalSet> page = hospitalSetService.page(hospitalSetPage, queryWrapper);
/*
        此时返回的page 等价于
        Page<HospitalSet> hospitalSetPage = new Page<>(pageNum,pageSize);
        中的   hospitalSetPage
*/
        List<HospitalSet> records = page.getRecords(); //当前页的结果集
        long size = page.getSize();//即每页显示的记录数
        long total = page.getTotal(); //总记录数
        long pages = page.getPages();// 总页数
        List<OrderItem> orders = page.getOrders(); //返回排序信息，即按照哪个字段进行排序，排序方式是升序还是降序
        long current = page.getCurrent(); //返回当前页码

        return R.ok().data("total", total).data("rows", records);
    }

    //医院的锁定和解锁

    @ApiOperation("根据id 修改状态值")
    @GetMapping("/lockHospitalSet/{id}/{status}")
    public R updateStatus(@ApiParam(name = "id", value = "医院id") @PathVariable Long id,
                          @ApiParam(name = "status", value = "将要修改的状态值") @PathVariable Integer status) {

        //先查询是否存在该医院
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet == null) {
            return R.err().message("医院未开通设置");
        }
        //判断status的取值范围
        if (status != 0 && status != 1) {
            return R.err().message("状态值有误");
        }
        if (status == hospitalSet.getStatus()) {
            return R.err().message("请勿重复操作");
        }
//更新
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b ? R.ok() : R.err();


    }

    //修改
    @ApiOperation("根据id修改医院信息")
    @RequestMapping(value = "/updatehospital", method = RequestMethod.POST)
    public R updateHospital(@ApiParam(name = "hospitalSet", value = "医院修改的信息", required = true) @RequestBody HospitalSet hospitalSet) {
//传入的医院的id 查询时否存在
        Long id = hospitalSet.getId();
        HospitalSet hp = hospitalSetService.getById(id);
        if (hp == null) {
            return R.err().message("修改的医院不存在");
        } else {
            boolean b = hospitalSetService.updateById(hospitalSet);
            return b ? R.ok().message("成功") : R.err().message("失败");
        }


    }


    // 新增
    @ApiOperation(value = "医院新增功能")
    @PostMapping("/savehospital")
    public R saveHospital(@ApiParam(name = "HospitalSet", value = "医院的实体类") @RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);

        boolean save = hospitalSetService.save(hospitalSet);
        return save ? R.ok().message("开通医院成功") : R.err().message("开通失败");

    }


    //查询
    @ApiOperation(value = "医院查询功能")
    @GetMapping("/findById/{id}")
    public R findById(@ApiParam(name = "id", value = "查询的医院的id") @PathVariable Long id) {

        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return R.ok().data("第一个数据", hospitalSet);
    }

    //    逻辑删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "医院逻辑删除功能")
    public R deleteById(@ApiParam(name = "id", value = "逻辑删除的的医院id") @PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        return b ? R.ok() : R.err();
    }

}
